package com.example.speeddating.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.speeddating.annotaion.LoginAdmin;
import com.example.speeddating.annotaion.LoginUser;
import com.example.speeddating.annotaion.TokenCheck;
import com.example.speeddating.common.BaseResponse;
import com.example.speeddating.common.RespGenerator;
import com.example.speeddating.dto.AdminDto;
import com.example.speeddating.dto.MatchDto;
import com.example.speeddating.dto.ModifyUserDto;
import com.example.speeddating.entity.Admin;
import com.example.speeddating.entity.TokenEntity;
import com.example.speeddating.entity.User;
import com.example.speeddating.exception.CommonException;
import com.example.speeddating.service.AdminService;
import com.example.speeddating.service.UserService;
import com.example.speeddating.utils.JwtConstant;
import com.example.speeddating.utils.JwtTokenUtil;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.*;

/**
 *
 * @author tina
 * @since 2023-07-19 07:13:59
 */

@Api(tags = "管理员接口")
@RestController
@Slf4j
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private UserService userService;

    /**
     * 管理员登录
     * @param admin
     * @return
     * @throws IOException
     */
    @PostMapping("/login")
    public BaseResponse<Admin> login(@RequestBody AdminDto admin) throws IOException {
        log.info("admin:{}", admin);
        String username = admin.getUsername();
        String password = admin.getPassword();
        Admin user = adminService.login(username, password);
        if (user==null){
           throw new CommonException("用户名或密码错误");
        }else {
//          生成token
            Map<String, Object> claims = new HashMap<>();
            claims.put(JwtConstant.USER_INFO_ID, user.getId());
            claims.put(JwtConstant.USER_INFO_USERNAME, user.getNickname());
            claims.put(JwtConstant.JWT_CREATE_TIME, new Date());
            String token = JwtTokenUtil.createToken(claims);
            HashMap<String, Object> map = new HashMap<>();
            map.put("user", user);
            map.put("token", token);
            return RespGenerator.returnOK(map);
        }
    }

    /**
     * 查询所有用户
     * @param admin
     * @param pageNum
     * @param pageSize
     * @return
     */
    @TokenCheck(roles = "admin")
    @GetMapping("/users")
    public BaseResponse<Page> selectAllUses(@LoginAdmin Admin admin, @RequestParam(value = "pageNum",defaultValue = "1") int pageNum , @RequestParam(value = "pageSize",defaultValue = "10") int pageSize){
        Page<User> page = new Page<>(pageNum, pageSize);
        return RespGenerator.returnOK(userService.selectAllUser(page));
    }

    @GetMapping("/get")
    public BaseResponse testgetMethod(@RequestParam(value = "name") String sname,int age){
        return RespGenerator.returnOK("我收到数据了"+sname+","+age);
    }

    /**
     * 修改状态
     * @param admin
     * @param id
     * @param status
     * @return
     */
    @TokenCheck(roles = "admin")
    @PutMapping("/change_status")
    public BaseResponse<Page> searchUser(@LoginAdmin Admin admin,Long id,Integer status){
        userService.updateUserStatus(id,status);
        return RespGenerator.returnOK("修改成功");
    }

    @TokenCheck(roles = "admin")
    @PostMapping("/user/modify")
    public BaseResponse<Page> modifyOneUses(@LoginAdmin Admin admin,@RequestBody ModifyUserDto userDto){
        log.info("userDto"+userDto);
        if (userDto==null||userDto.getId()==null||userDto.getNickname()==null||userDto.getPhone()==null||userDto.getSex()==null||userDto.getProfessionId()==null){
            throw new CommonException("请求参数错误");
        }
        User user = new User();
        user.setId(userDto.getId());
        user.setNickname(userDto.getNickname());
        user.setPhone(userDto.getPhone());
        user.setSex(userDto.getSex());
        user.setProfessionId(userDto.getProfessionId());
        userService.updateUser(user);
        return RespGenerator.returnOK("修改成功");
    }


    @TokenCheck(roles = "admin")
    @DeleteMapping("/user/delete/{id}")
    public BaseResponse<Page> deleteOneUses(@LoginAdmin Admin admin,@PathVariable Long id){
        userService.deleteUserById(id);
        return RespGenerator.returnOK("删除成功");
    }

    @TokenCheck(roles = "admin")
    @GetMapping("/user/screen/nav")
    public BaseResponse<HashMap<String,Object>> getScreenNav(@LoginAdmin Admin admin){
        HashMap<String,Object> nav= userService.getScreenNav();
        return RespGenerator.returnOK(nav);
    }

    @TokenCheck(roles = "admin")
    @GetMapping("/user/screen/match")
    public BaseResponse<HashMap<String,Object>> getScreenMatch(@LoginAdmin Admin admin){
        List<MatchDto> screenMatch = userService.getScreenMatch();
        return RespGenerator.returnOK(screenMatch);
    }


}
